package com.lz.ttapi;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;

import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdConstant;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTAppDownloadListener;
import com.bytedance.sdk.openadsdk.TTSplashAd;
import com.fyhdxxl.mz.R;
import com.lz.uikit.Constants;
import com.lz.uikit.UIUtils;

import org.cocos2dx.javascript.AppActivity;

public class TTSpashAd {
    public static final String TAG = "TTSplash";
    public  static  AppActivity activity ;
    private static TTAdNative mTTAdNative;
    private static FrameLayout mSplashContainer;
    private static final int AD_TIME_OUT = 3000;
    private static boolean mIsExpress = false; //是否请求模板广告

    public static void init(Context content){
        activity = (AppActivity)content;
        //step2:创建TTAdNative对象，createAdNative(Context context) banner广告context需要传入Activity对象
        mTTAdNative = TTAdManagerHolder.get().createAdNative(content);
        //step3:(可选，强烈建议在合适的时机调用):申请部分权限，如read_phone_state,防止获取不了imei时候，下载类广告没有填充的问题。
        TTAdManagerHolder.get().requestPermissionIfNecessary(content);
        mSplashContainer = (FrameLayout) activity.findViewById(R.id.splash_container);
        if(activity.getMake() == 2){
            loadSplashAd();
        }else{
            activity.setMake();
        }
    }

    public static void loadSplashAd() {
        //step3:创建开屏广告请求参数AdSlot,具体参数含义参考文档
        AdSlot adSlot = null;
        if (mIsExpress) {
            //个性化模板广告需要传入期望广告view的宽、高，单位dp，请传入实际需要的大小，
            //比如：广告下方拼接logo、适配刘海屏等，需要考虑实际广告大小
            float expressViewWidth = UIUtils.getScreenWidthDp(activity);
            float expressViewHeight = UIUtils.getHeight(activity);
            adSlot = new AdSlot.Builder()
                    .setCodeId(Constants.TT_SPLASH_ID)
                    .setSupportDeepLink(true)
                    .setImageAcceptedSize(1080, 1920)
                    //模板广告需要设置期望个性化模板广告的大小,单位dp,代码位是否属于个性化模板广告，请在穿山甲平台查看
                    .setExpressViewAcceptedSize(expressViewWidth, expressViewHeight)
                    .build();
        } else {
            adSlot = new AdSlot.Builder()
                    .setCodeId(Constants.TT_SPLASH_ID)
                    .setSupportDeepLink(true)
                    .setImageAcceptedSize(1080, 1920)
                    .build();
        }

        //step4:请求广告，调用开屏广告异步请求接口，对请求回调的广告作渲染处理
        mTTAdNative.loadSplashAd(adSlot, new TTAdNative.SplashAdListener() {
            @Override
            public void onError(int code, String message) {
                showToast(String.valueOf(message));
            }

            @Override
            public void onTimeout() {
                showToast("开屏广告加载超时");
                hideSpashAd();
            }

            @Override
            public void onSplashAdLoad(TTSplashAd ad) {
                showToast("开屏广告请求成功");
                if (ad == null) {
                    return;
                }

                //获取SplashView
                View view = ad.getSplashView();
                if (view != null && mSplashContainer != null) {
                    System.out.println("创建");
                    showSpashAd();
                    mSplashContainer.removeAllViews();
                    //把SplashView 添加到ViewGroup中,注意开屏广告view：width >=70%屏幕宽；height >=50%屏幕高
                    mSplashContainer.addView(view);
                    //设置不开启开屏广告倒计时功能以及不显示跳过按钮,如果这么设置，您需要自定义倒计时逻辑
                    //ad.setNotAllowSdkCountdown();
                }else{
                    System.out.println("没有创建");
                }

                ad.setSplashInteractionListener(new TTSplashAd.AdInteractionListener() {
                    @Override
                    public void onAdClicked(View view, int i) {

                    }

                    @Override
                    public void onAdShow(View view, int i) {
                    }

                    @Override
                    public void onAdSkip() {
                        showToast("开屏广告跳过");
                        //mSplashContainer.removeAllViews();
                        hideSpashAd();

                    }

                    @Override
                    public void onAdTimeOver() {
                        showToast("开屏广告倒计时结束");
                        //mSplashContainer.removeAllViews();
                        hideSpashAd();
                    }
                });


                if(ad.getInteractionType() == TTAdConstant.INTERACTION_TYPE_DOWNLOAD) {
                    ad.setDownloadListener(new TTAppDownloadListener() {
                        @Override
                        public void onIdle() {

                        }

                        @Override
                        public void onDownloadActive(long l, long l1, String s, String s1) {

                        }

                        @Override
                        public void onDownloadPaused(long l, long l1, String s, String s1) {

                        }

                        @Override
                        public void onDownloadFailed(long l, long l1, String s, String s1) {

                        }

                        @Override
                        public void onDownloadFinished(long l, String s, String s1) {

                        }

                        @Override
                        public void onInstalled(String s, String s1) {

                        }
                    });
                }
            }
        },AD_TIME_OUT);
    }

    public static void showSpashAd() {
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mSplashContainer.setVisibility(View.VISIBLE);
            }
        });
    }

    public static void hideSpashAd() {
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mSplashContainer.setVisibility(View.GONE);
            }
        });
    }

    public static void onDestroy() {
        System.out.println("开屏销毁");
        activity.removeSplashView();

    }

    private static void showToast(String msg) {
        System.out.println("TAG"+msg);
    }


    public static void openSplash() {
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                loadSplashAd();
            }
        });
    }
}
